package com.kakarote.admin.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.admin.common.AdminCodeEnum;
import com.kakarote.core.common.enums.AdminRoleTypeEnum;
import com.kakarote.admin.entity.PO.AdminConfig;
import com.kakarote.admin.entity.PO.AdminLanguagePackRel;
import com.kakarote.admin.entity.PO.AdminMenu;
import com.kakarote.admin.entity.PO.AdminRole;
import com.kakarote.admin.entity.VO.AdminRoleVO;
import com.kakarote.admin.entity.VO.ModuleSettingVO;
import com.kakarote.admin.mapper.AdminLanguagePackRelMapper;
import com.kakarote.admin.service.IAdminConfigService;
import com.kakarote.admin.service.IAdminLanguagePackRelService;
import com.kakarote.admin.service.IAdminMenuService;
import com.kakarote.admin.service.IAdminRoleService;
import com.kakarote.core.common.enums.LanguageFieldEnum;
import com.kakarote.core.common.enums.SystemCodeEnum;
import com.kakarote.core.feign.admin.entity.AdminLanguagePackRelAddBO;
import com.kakarote.core.feign.admin.entity.AdminLanguagePackRelBO;
import com.kakarote.core.feign.admin.entity.AdminMessageEnum;
import com.kakarote.core.feign.examine.service.ExamineService;
import com.kakarote.core.feign.oa.OaService;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.core.utils.AdminLanguageUtil;
import com.kakarote.hrm.feign.service.HrmService;;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 语言包关系表
 * @company: 卡卡罗特软件科技有限公司
 * @author: zyh
 * @date: 2022/6/6 17:09
 * @version: @Version: V1.0
 */
@Slf4j
@Service
public class AdminLanguagePackRelServiceImpl extends BaseServiceImpl<AdminLanguagePackRelMapper, AdminLanguagePackRel> implements IAdminLanguagePackRelService {


    @Autowired
    private IAdminConfigService adminConfigService;


    @Autowired
    private IAdminRoleService adminRoleService;

    @Autowired
    private ExamineService examineService;

    @Autowired
    private OaService oaService;

    @Autowired
    private HrmService hrmService;


    @Autowired
    private IAdminMenuService adminMenuService;

    /**
     * 功能描述: <br>
     * 〈查询关系key〉
     *
     * @param: adminLanguagePackRelBO
     * @return: java.util.List<com.kakarote.admin.entity.PO.AdminLanguagePackRel>
     * @throws:
     * @author: zyh
     * @date: 2022/6/6 17:09
     */
    @Override
    public List<AdminLanguagePackRel> queryLanguageKeyByBO(AdminLanguagePackRelBO adminLanguagePackRelBO) {
        return this.getBaseMapper().queryLanguagePackRelList(adminLanguagePackRelBO);
    }

    /**
     * 功能描述: <br>
     * 〈批量添加修改〉
     *
     * @param: adminLanguagePackRels
     * @return: void
     * @throws:
     * @author: zyh
     * @date: 2022/6/6 17:12
     */
    @Override
    public void addOrUpdateLanguagePack(AdminLanguagePackRelAddBO relAddBO) {
        List<AdminLanguagePackRelBO> adminLanguagePackRels = relAddBO.getRelBOList();
        if (CollectionUtil.isEmpty(relAddBO.getRelBOList())) {
            return;
        }
        List<AdminLanguagePackRel> rels = BeanUtil.copyToList(adminLanguagePackRels, AdminLanguagePackRel.class);
        saveOrUpdateBatch(rels);
    }

    /**
     * 功能描述: <br>
     * 〈批量添加修改〉
     *
     * @param: adminLanguagePackRels
     * @return: void
     * @throws:
     * @author: zyh
     * @date: 2022/6/6 17:12
     */
    @Override
    public List<Map<String, Object>> getAllLanguagePackRel() {
        //获取admin语言Rel
        List<Map<String, Object>> adminRel = getAdminRel();
        //获取crm语言Rel
        List<Map<String, Object>> crmRel = getCrmRel();
        if (CollectionUtil.isNotEmpty(crmRel)) {
            adminRel.addAll(crmRel);
        }
        //获取examine语言Rel
        List<Map<String, Object>> examineRel = getExamineRel();
        if (CollectionUtil.isNotEmpty(examineRel)) {
            adminRel.addAll(examineRel);
        }
        //获取oa语言Rel
        List<Map<String, Object>> oaRel = getOaRel();
        if (CollectionUtil.isNotEmpty(oaRel)) {
            adminRel.addAll(oaRel);
        }
        //获取hrm语言Rel
        List<Map<String, Object>> hrmRel = getHrmRel();
        if (CollectionUtil.isNotEmpty(hrmRel)) {
            adminRel.addAll(hrmRel);
        }
        //获取jxc语言Rel
        List<Map<String, Object>> jxcRel = getJxcRel();
        if (CollectionUtil.isNotEmpty(jxcRel)) {
            adminRel.addAll(jxcRel);
        }
        //获取work语言Rel
        List<Map<String, Object>> workRel = getWorkRel();
        if (CollectionUtil.isNotEmpty(workRel)) {
            adminRel.addAll(workRel);
        }
        //获取财务语言Rel
        List<Map<String, Object>> financeRel = getFinanceRel();
        if (CollectionUtil.isNotEmpty(financeRel)) {
            adminRel.addAll(financeRel);
        }
        //获取非标准自定义Rel
        List<Map<String, Object>> packRel = getPackRel();
        if (CollectionUtil.isNotEmpty(packRel)) {
            adminRel.addAll(packRel);
        }
        //获取km语言Rel
        List<Map<String, Object>> kmRel = getKmRel();
        if (CollectionUtil.isNotEmpty(kmRel)) {
            adminRel.addAll(kmRel);
        }
        return adminRel;
    }

    /**
     * 功能描述: <br>
     * 〈获取admin模块所需语言key信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/6/23 16:57
     */
    public List<Map<String, Object>> getAdminRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        //配置信息
        List<Map<String, Object>> adminConfigRel = getAdminConfigRel();
        if (CollectionUtil.isNotEmpty(adminConfigRel)) {
            listMap.addAll(adminConfigRel);
        }
        //菜单信息
        List<Map<String, Object>> menuRel = getMenuRel();
        if (CollectionUtil.isNotEmpty(menuRel)) {
            listMap.addAll(menuRel);
        }
        //角色类型信息
        List<Map<String, Object>> roleTypeRel = getRoleTypeRel();
        if (CollectionUtil.isNotEmpty(roleTypeRel)) {
            listMap.addAll(roleTypeRel);
        }
        //消息信息
        List<Map<String, Object>> messageRel = getMessageRel();
        if (CollectionUtil.isNotEmpty(messageRel)) {
            listMap.addAll(messageRel);
        }
        //角色信息
        List<Map<String, Object>> typeChildRel = getRoleTypeChildRel();
        if (CollectionUtil.isNotEmpty(typeChildRel)) {
            listMap.addAll(typeChildRel);
        }
        //补充信息
        List<Map<String, Object>> enumRel = getEnumRel();
        if (CollectionUtil.isNotEmpty(enumRel)) {
            listMap.addAll(enumRel);
        }
        //错误信息
        List<Map<String, Object>> errorRel = getErrorRel();
        if (CollectionUtil.isNotEmpty(errorRel)) {
            listMap.addAll(errorRel);
        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈获取crm模块所需语言key信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/6/23 16:58
     */
    public List<Map<String, Object>> getCrmRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈获取流程模块所需语言key信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/6/23 16:58
     */
    public List<Map<String, Object>> getExamineRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        List<Map<String, Object>> mapList = examineService.getAllFieldLanguageRel().getData();
        if (ObjectUtil.isNotEmpty(mapList) && CollectionUtil.isNotEmpty(mapList)) {
            listMap.addAll(mapList);
        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈获取oa模块所需语言key信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/6/23 16:58
     */
    public List<Map<String, Object>> getOaRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        List<Map<String, Object>> mapList = oaService.getAllFieldLanguageRel().getData();
        if (ObjectUtil.isNotEmpty(mapList) && CollectionUtil.isNotEmpty(mapList)) {
            listMap.addAll(mapList);
        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈获取hrm模块所需语言key信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/6/23 16:58
     */
    public List<Map<String, Object>> getHrmRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        List<Map<String, Object>> mapList = hrmService.getAllFieldLanguageRel().getData();
        if (ObjectUtil.isNotEmpty(mapList) && CollectionUtil.isNotEmpty(mapList)) {
            listMap.addAll(mapList);
        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈jxc〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/6/23 16:58
     */
    public List<Map<String, Object>> getJxcRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈work模块〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/6/23 16:58
     */
    public List<Map<String, Object>> getWorkRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈财务模块〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/6/23 16:58
     */
    public List<Map<String, Object>> getFinanceRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈知识库模块〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/6/23 16:58
     */
    public List<Map<String, Object>> getKmRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈配置信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/7/26 0:11
     */
    public List<Map<String, Object>> getAdminConfigRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        List<ModuleSettingVO> moduleSettingVOS = adminConfigService.queryModuleSetting();
        if (CollectionUtil.isEmpty(moduleSettingVOS)) {
            return listMap;
        }
        for (ModuleSettingVO settingVO : moduleSettingVOS) {
            Map<String, String> languageKeyMap = settingVO.getLanguageKeyMap();
            if (CollectionUtil.isNotEmpty(languageKeyMap)) {
                languageKeyMap.forEach((key, value) -> {
                    Map<String, Object> record = new HashMap<>();
                    record.put("fileName", value);
                    record.put("chinese", settingVO.getName());
                    record.put("translateName", "");
                    listMap.add(record);
                });
            }

        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈菜单信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/7/26 0:11
     */
    public List<Map<String, Object>> getMenuRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        List<AdminMenu> menulist = adminMenuService.list();
        if (CollectionUtil.isNotEmpty(menulist)) {
            for (AdminMenu adminMenu : menulist) {
                Map<String, Object> keyMap = new HashMap<>();
                keyMap.put("fileName", "promission.system." + adminMenu.getMenuId());
                keyMap.put("chinese", adminMenu.getMenuName());
                keyMap.put("translateName", "");
                listMap.add(keyMap);
            }
        }

        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈角色类型信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/7/26 0:11
     */
    public List<Map<String, Object>> getRoleTypeRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        listMap.add(new JSONObject().fluentPut("fileName", "menuRoles1.系统管理角色").fluentPut("chinese", "系统管理角色").fluentPut("translateName", ""));
        listMap.add(new JSONObject().fluentPut("fileName", "menuRoles7.办公管理角色").fluentPut("chinese", "办公管理角色").fluentPut("translateName", ""));
        listMap.add(new JSONObject().fluentPut("fileName", "menuRoles2.客户管理角色").fluentPut("chinese", "客户管理角色").fluentPut("translateName", ""));
        List<com.kakarote.admin.entity.PO.AdminConfig> adminConfigs = adminConfigService.queryConfigListByName(AdminRoleTypeEnum.HRM.getName(), AdminRoleTypeEnum.FINANCE.getName(), AdminRoleTypeEnum.JXC.getName(), AdminRoleTypeEnum.SCRM.getName());
        Map<String, List<com.kakarote.admin.entity.PO.AdminConfig>> adminMap = adminConfigs.stream().collect(Collectors.groupingBy(AdminConfig::getName));
        if (adminMap.containsKey(AdminRoleTypeEnum.HRM.getName()) && adminMap.get(AdminRoleTypeEnum.HRM.getName()).get(0).getStatus() != 0) {
            listMap.add(new JSONObject().fluentPut("fileName", "menuRoles9.人力资源管理角色").fluentPut("chinese", "人力资源管理角色").fluentPut("translateName", ""));
        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈角色信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/7/26 0:10
     */
    public List<Map<String, Object>> getRoleTypeChildRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        List<AdminRoleVO> allRoleList = adminRoleService.getAllRoleList();
        if (CollectionUtil.isEmpty(allRoleList)) {
            return listMap;
        }
        for (AdminRoleVO roleVO : allRoleList) {
            List<AdminRole> list = roleVO.getList();
            for (AdminRole role : list) {
                Map<String, String> languageKeyMap = role.getLanguageKeyMap();
                languageKeyMap.forEach((item, value) -> {
                    listMap.add(new JSONObject().fluentPut("fileName", value).fluentPut("chinese", role.getRoleName()).fluentPut("translateName", ""));
                });
            }
        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈字段枚举翻译信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/7/26 0:10
     */
    public List<Map<String, Object>> getEnumRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        for (LanguageFieldEnum fieldEnum : LanguageFieldEnum.values()) {
            listMap.add(new JSONObject().fluentPut("fileName", fieldEnum.getField()).fluentPut("chinese", fieldEnum.getModule()).fluentPut("translateName", ""));
        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈消息需要翻译信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/7/26 0:09
     */
    public List<Map<String, Object>> getMessageRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        for (AdminMessageEnum messageEnum : AdminMessageEnum.values()) {
            Map<String, Object> keyMap = new HashMap<>();
            keyMap.put("fileName", "admin.config.message." + messageEnum.getRemarks());
            keyMap.put("chinese", messageEnum.getRemarks());
            keyMap.put("translateName", "");
            listMap.add(keyMap);

        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈错误信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/7/26 0:09
     */
    public List<Map<String, Object>> getErrorRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        for (AdminCodeEnum adminCodeEnum : AdminCodeEnum.values()) {
            Map<String, Object> keyMap = new HashMap<>();
            keyMap.put("fileName", "error." + adminCodeEnum.getCode());
            keyMap.put("chinese", AdminLanguageUtil.replaceMsg(adminCodeEnum.getMsg()));
            keyMap.put("translateName", "");
            listMap.add(keyMap);

        }
        for (SystemCodeEnum sysCodeEnum : SystemCodeEnum.values()) {
            Map<String, Object> keyMap = new HashMap<>();
            keyMap.put("fileName", "error." + sysCodeEnum.getCode());
            keyMap.put("chinese", AdminLanguageUtil.replaceMsg(sysCodeEnum.getMsg()));
            keyMap.put("translateName", "");
            listMap.add(keyMap);

        }
        return listMap;
    }

    /**
     * 功能描述: <br>
     * 〈获取数据库自定义信息〉
     *
     * @param:
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws:
     * @author: zyh
     * @date: 2022/7/26 0:07
     */
    public List<Map<String, Object>> getPackRel() {
        List<Map<String, Object>> listMap = new ArrayList<>();
        //获取系统默认的
        AdminLanguagePackRelBO relBO = new AdminLanguagePackRelBO();
        relBO.setLanguagePackType(1);
        List<AdminLanguagePackRel> rels = getBaseMapper().queryLanguagePackRelList(relBO);
        for (AdminLanguagePackRel roleVO : rels) {
            listMap.add(new JSONObject().fluentPut("fileName", roleVO.getLanguagePackRelKey()).fluentPut("chinese", roleVO.getLanguagePackRelName()).fluentPut("translateName", ""));
        }
        return listMap;
    }


}
